import Vue from "vue"
import VueRouter from "vue-router"
import Login from "../components/Login.vue"
import Home from "../components/home.vue"
import Welcome from "../components/welcome.vue"
import Users from "../components/users.vue"
import Rights from "../components/power/Rights.vue"
import Roles from "../components/power/Roles.vue"
import Cate from "../components/goods/cate.vue"
import Params from "../components/goods/params.vue"
import Goods from "../components/goods/goods.vue"
import Add from "../components/goods/add.vue"
import Order from "../components/order/orders.vue"
import Report from "../components/report/reports.vue"
// 富文本框
import TreeTable from "vue-table-with-tree-grid"

Vue.use(VueRouter)
Vue.component("tree-table", TreeTable)
const routes = [
    // 重定向组件
    { path: "/", redirect: "/Login" },
    { path: "/login", component: Login },
    {
        path: "/home",
        component: Home,
        redirect: "/welcome",
        children: [
            { path: "/welcome", component: Welcome },
            { path: "/users", component: Users },
            { path: "/Rights", component: Rights },
            { path: "/Roles", component: Roles },
            { path: "/categories", component: Cate },
            { path: "/params", component: Params },
            { path: "/goods", component: Goods },
            { path: "/goods/add", component: Add },
            { path: "/orders", component: Order },
            { path: "/reports", component: Report }
        ]
    }
]

const router = new VueRouter({
    mode: "history",
    base: process.env.BASE_URL,
    routes
})

// 挂载路由导航守卫 来控制我们页面的访问权限
router.beforeEach((to, from, next) => {
    // to 代表要访问的路径
    //  from 代表从哪个路径跳转过来的，
    //  next一个函数，next()表示放行  next('/login') 强制跳转
    if (to.path === "/Login") return next()
        // 获取token
    const token = window.sessionStorage.getItem("token")
    if (!token) return next("./Login")
    next()
})
export default router